package xiaohong.data.stack;

public class StackLinkedDemo {

    public static void main(String[] args) {
        StackLinked stackLinked = new StackLinked();
        StackEntity stack = new StackEntity(1 , "最后");
        StackEntity stack1 = new StackEntity(2 , "小宏");
        StackEntity stack2 = new StackEntity(3 , "拦路雨");
        StackEntity stack3 = new StackEntity(4 , "钢铁侠");
        StackEntity stack4 = new StackEntity(5 , "蜘蛛侠");
        StackEntity stack5 = new StackEntity(6 , "美国队长");
        StackEntity stack6 = new StackEntity(7 , "黑寡妇");
        stackLinked.push(stack);
        stackLinked.push(stack1);
        stackLinked.push(stack2);
        stackLinked.push(stack3);
        stackLinked.push(stack4);
        stackLinked.push(stack5);
        stackLinked.push(stack6);

        stackLinked.list();

        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();
        stackLinked.pop();


    }



}

class StackLinked {

//    StackEntity stack = new StackEntity();
    StackEntity lastStack = new StackEntity();

    public StackLinked() {
//        stack.setNext(lastStack);
    }

    public boolean isEmpty(){
        if (lastStack.getPre() == null){
            System.out.println("stack is empty");
        }
        return lastStack.getPre() == null;
    }

    public void push(StackEntity stackPoint){
        StackEntity stackEntity = lastStack.getPre();
        lastStack.setPre(stackPoint);
        if (stackEntity != null){
            lastStack.getPre().setPre(stackEntity);
        }
    }

    public void list(){
        StackEntity stackEntity = lastStack.getPre();
        while (stackEntity != null){
            System.out.println(stackEntity);
            stackEntity = stackEntity.getPre();
        }
    }


    public StackEntity pop(){
        if (isEmpty()){
            return null;
        }
        StackEntity stackEntity = lastStack.getPre();
        StackEntity stack = new StackEntity();
        if (stackEntity != null){
            stack = stackEntity;
            lastStack = stackEntity;
        }
        System.out.println("出栈的数据是" + stack);
        return stack;
    }



}



class StackEntity{

    private int no ;
    private String data ;
    private StackEntity pre ;
//    private StackEntity next ;

    public StackEntity(int no, String data) {
        this.no = no;
        this.data = data;
    }

    public StackEntity() {
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public StackEntity getPre() {
        return pre;
    }

    public void setPre(StackEntity pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return "StackEntity{" +
                "no=" + no +
                ", data='" + data + '\'' +
                '}';
    }

    //    public StackEntity getNext() {
//        return next;
//    }
//
//    public void setNext(StackEntity next) {
//        this.next = next;
//    }

}